<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 商品列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-select v-model="queryType" placeholder="类型" class="handle-select mr10">
                    <el-option key="1" label="进行中" value="0"></el-option>
                    <el-option key="2" label="已取消" value="1"></el-option>
                    <el-option key="3" label="已完结" value="2"></el-option>
                    <el-option key="4" label="待支付" value="3"></el-option>
                </el-select>
                <el-input v-model="queryName" placeholder="用户名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>

            <div class="column">
                <div class="ordeCard" v-for="(order, index) in viewList" :key="index">
                    <el-alert
                        style="margin-bottom: 0.5rem"
                        v-if="order.state == 0"
                        title="进行中"
                        effect="dark"
                        type="warning"
                        :closable="false"
                    >
                    </el-alert>
                    <el-alert
                        style="margin-bottom: 0.5rem"
                        v-if="order.state == 1"
                        title="已取消"
                        effect="dark"
                        type="info"
                        :closable="false"
                    >
                    </el-alert>
                    <el-alert
                        style="margin-bottom: 0.5rem"
                        v-if="order.state == 2"
                        title="已完结"
                        effect="dark"
                        type="success"
                        :closable="false"
                    >
                    </el-alert>
                    <el-alert
                        style="margin-bottom: 0.5rem"
                        v-if="order.state == 3"
                        title="待支付"
                        effect="dark"
                        type="error"
                        :closable="false"
                    >
                    </el-alert>
                    <div class="title row center">
                        <span class="font16" style="margin-right: 10px">订单编号：</span>
                        <span style="margin-right: 30px">{{ order.orderId }}</span>
                        <span class="font16" style="margin-right: 10px">订单编号：</span>
                        <span style="margin-right: 30px">{{ order.orderId }}</span>
                    </div>
                    <div class="title row center">
                        <span class="font16" style="margin-right: 10px">用户：{{ order.user.userName }}</span>
                        <div class="address row">
                            <i class="el-icon-s-comment font16"></i>
                            <div class="text">{{ order.remark }}</div>
                        </div>
                    </div>
                    <div class="title row center">
                        <span class="font16" style="margin-right: 10px">店面：{{ order.store.storeName }}</span>
                        <div class="address row">
                            <i class="el-icon-location font16"></i>
                            <div class="text">{{ order.store.address }}</div>
                        </div>
                    </div>
                    <div class="divide"></div>
                    <div class="roomList column">
                        <div class="room row" v-for="item in order.commodityList" :key="item.id">
                            <div class="column" style="border-right: 1px solid #dadfe6">
                                <el-image style="height: 71px; width: 100px; margin-right: 24px" :src="item.imgUrl" fit="cover"></el-image>
                                <div class="roomname">{{ item.commodityName }}</div>
                                <div class="seeroom">查看商品信息</div>
                            </div>
                            <div class="adult row">x {{ item.count }}</div>
                            <div class="bedfacility column font14" style="width: 150px">
                                <div class="bed underline">{{ item.properties }}</div>
                            </div>
                            <div class="bedfacility column font14">
                                <div class="bed" v-for="(f, index) in item.otherfacility" :key="index">
                                    {{ f }}
                                </div>
                            </div>
                            <div class="bedfacility column font14">
                                <div class="bed underline">30分钟内可免费取消</div>
                            </div>
                            <div class="salecard-price row">
                                <div class="salecard-price-panel">￥{{ item.price }}</div>
                                <div class="salecard-price-button">
                                    <div class="paytype">在线付</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="divide"></div>
                    <div class="totalPrice">
                        <span class="label">优惠券：</span>
                        <span v-if="order.couponId == undefined">无</span>
                        <span v-else> 新用户首单优惠卷 -￥1.5</span>
                    </div>
                    <div class="totalPrice">
                        <span class="label">支付方式：</span>
                        <span v-if="order.payWay == 1">微信支付</span>
                        <span v-else> 支付宝支付</span>
                    </div>
                    <div class="totalPrice">
                        <span class="label">总价</span>&nbsp;<span class="value"> ￥{{ order.totalPrice }}</span>
                    </div>
                    <div class="divide"></div>
                    <div class="btns">
                        <el-button @click="handleDelete(index)" v-if="order.state == '1' || order.state == '2'">删除</el-button
                        ><el-button>在线咨询</el-button><el-button @click="wangjie(order)" v-if="order.state == '0'">完结</el-button
                        ><el-button v-if="order.state == '1'">取消</el-button>
                    </div>
                </div>
            </div>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import commodityApi from '@/api/commodity';
export default {
    name: 'order',
    data() {
        return {
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            queryType: '',
            queryName: '',
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            viewList: [],
            orderList: [
                // {
                //     orderId: 'ae9fc519f21546679c0d22acfac81826',
                //     hotelName: '上海圆谷民宿',
                //     address: '中国，上海，浦东新区，陈桥村957号',
                //     totalPrice: '227',
                //     orderType: '0',
                //     roomList: [
                //         {
                //             id: 1,
                //             imgUrl: require('@/assets/img/room/1.jpg'),
                //             roomName: 'A栋超级英雄房',
                //             adult: 2,
                //             bed: '1张双人床',
                //             facility: '2份早餐',
                //             otherfacility: ['禁烟', '有窗'],
                //             price: 227,
                //             number: 1
                //         }
                //     ]
                // }
            ]
        };
    },
    created() {
        this.getData();
    },
    watch: {
        queryType(newValue, oldValue) {
            this.viewList = this.orderList.filter((order) => {
                return order.state == newValue;
            });
        },
        queryName(newValue, oldValue) {
            this.viewList = this.orderList.filter((order) => {
                return order.user.userName.indexOf(newValue) != -1;
            });
        }
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.viewList = [];
            commodityApi.getOrders().then((res) => {
                console.log(res.data.data);
                var data = res.data.data;
                var currentOrder = data.currentOrder.filter((order) => {
                    return order.isTakeSelf == 1;
                });
                var waitPayOrder = data.waitPayOrder.filter((order) => {
                    return order.isTakeSelf == 1;
                });
                var finishOrder = data.finishOrder.filter((order) => {
                    return order.isTakeSelf == 1;
                });
                var cancelOrder = data.cancelOrder.filter((order) => {
                    return order.isTakeSelf == 1;
                });
                for (let index = 0; index < currentOrder.length; index++) {
                    this.orderList.push(data.currentOrder[index]);
                }
                for (let index = 0; index < waitPayOrder.length; index++) {
                    this.orderList.push(data.waitPayOrder[index]);
                }
                for (let index = 0; index < finishOrder.length; index++) {
                    this.orderList.push(data.finishOrder[index]);
                }
                for (let index = 0; index < cancelOrder.length; index++) {
                    this.orderList.push(data.cancelOrder[index]);
                }
                this.viewList = this.orderList;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('删除成功');
                    this.viewList.splice(index, 1);
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        wangjie(order) {
            this.$confirm('确定已完结吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.success('操作成功');
                    var formData = new FormData();
                    formData.append('orderId', order.orderId);
                    commodityApi.changeOrderToComplate(formData).then((res) => {
                        this.orderList = []
                        this.getData();
                    });
                })
                .catch(() => {});
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.container {
    background-color: #f7f7f7;
}
/* 订单卡片 */
.ordeCard {
    width: 100%;
    min-height: 100px;
    background-color: #fff;
    padding: 24px;
    position: relative;
    margin-bottom: 8px;
    box-sizing: border-box;
}
.title {
    color: #0f294d;
    font-weight: 700;
    letter-spacing: 0;
    height: 18px;
    margin-bottom: 8px;
}
.address {
    align-items: center;
}
.text {
    color: #0f294d;
    font-size: 14px;
    font-weight: 400;
    letter-spacing: 0;
    margin-left: 5px;
}
/* 房间列表 */
.room {
    padding-top: 16px;
    position: relative;
    background: #fff;
    margin-top: 4px;
    padding: 16px 24px 22px;
}
.roomname {
    width: 100px;
    font-weight: 600;
    font-size: 14px;
    line-height: 18px;
    color: #0f294d;
    padding: 6px 0;
}
.seeroom {
    font-size: 12px;
    color: #287dfa;
    line-height: 12px;
    text-align: left;
    font-weight: 600;
    cursor: pointer;
}
.adult {
    width: 100px;
    padding: 22px 0 0 24px;
}
.bedfacility {
    width: 150px;
    padding: 22px 30px 0 30px;
}
.bed {
    margin-bottom: 6px;
    width: 300px;
    line-height: 18px;
    vertical-align: baseline;
    color: #0f294d;
}
.facility {
    color: rgb(6, 174, 189);
}
.salecard-price {
    align-items: right;
    margin-left: 320px;
}
.salecard-price-panel {
    line-height: 28px;
    color: #287dfa;
    font-size: 24px;
    display: inline-block;
    padding-bottom: 4px;
    font-weight: 600;
    margin-right: 16px;
}
.book-box {
    text-align: center;
    font-size: 16px;
    line-height: 38px;
    background-color: #ff9500;
    text-align: center;
    height: 38px;
    justify-content: center;
    align-items: center;
    border-radius: 2px;
    font-weight: 700;
    padding: 0 24px;
    box-sizing: border-box;
    min-width: 75px;
    display: inline-block;
    color: #fff;
}
.paytype {
    text-align: center;
    font-size: 14px;
    font-weight: 600;
    color: #455873;
    line-height: 20px;
    margin-top: 8px;
}
.roomhold {
    text-align: center;
    clear: both;
    color: #ee3b28;
    font-size: 14px;
    font-weight: 400;
    height: 18px;
}
/* 总价 */
.totalPrice {
    line-height: 28px;
    text-align: right;
    margin-right: 120px;
}
.value {
    color: #287dfa;
    font-size: 24px;
    font-weight: 600;
}
.label {
    font-size: 14px;
    font-weight: 600;
    color: #455873;
}
.btns {
    margin-top: 24px;
}
</style>
